package com.hrt.datasender.web.heatbeat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.hrt.datasender.process.heartbeat.HeartbeatLoader;
import com.hrt.datasender.process.heartbeat.IHeartbeat;

@Service
public final class HeatBeatService {

    private static volatile HeatBeatService instance;

    private HeatBeatService() {

    }

    public static  HeatBeatService getInstance() {
        if (instance == null) {
            synchronized (HeatBeatService.class) {
                if (instance == null) {
                    instance = new HeatBeatService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取系统中所有存在的协议
     * 
     * @return
     * @throws IOException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
    public List<HeatBeatModel> getExistProtocol()
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {

        List<IHeartbeat> heartbeats = HeartbeatLoader.getInstance().getExistHeartbeats();
        List<HeatBeatModel> models = new ArrayList<>();

        for (IHeartbeat heartbeat : heartbeats) {
            HeatBeatModel model = new HeatBeatModel(heartbeat.getHeartbeatId(), heartbeat.getName());
            models.add(model);
        }

        return models;
    }

}
